package com.vf.admin.miaodong.service.impl;

import com.vf.admin.client.service.AppMingXiShouLiService;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.dao.AfterMingxiJinbiDao;
import com.vf.admin.miaodong.dao.AfterUserDao;
import com.vf.admin.miaodong.entity.AppMingxiJinbi;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.service.AfterMingxiJinbiService;
import com.vf.admin.utils.DatesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 账单明细-金币
 */
@Service
public class AfterMingxiJinbiServiceImpl implements AfterMingxiJinbiService {
    @Autowired
    private AfterMingxiJinbiDao afterMingxiJinbiDao;
    @Autowired
    private AfterUserDao afterUserDao;
    @Autowired
    AppMingXiShouLiService appMingXiShouLiService;

    /**
     * 查询列表
     */
    @Override
    public List<AppMingxiJinbi> findList(Map<String, Object> paraMap, SplitPageBean splitPage) {
        return afterMingxiJinbiDao.findList(paraMap, splitPage);
    }

    @Override
    public Map<String, Object> findListSum(Map<String, Object> paraMap) {
        Map<String, Object> data = new HashMap<>();
        List<AppMingxiJinbi> listSum = afterMingxiJinbiDao.findListSum(paraMap);
        double add = listSum.stream().filter(e -> e.getLeixing() == 1).mapToDouble(e -> Double.valueOf(e.getNum().stripTrailingZeros().toPlainString())).sum();
        double reduce = listSum.stream().filter(e -> e.getLeixing() == 2).mapToDouble(e -> Double.valueOf(e.getNum().stripTrailingZeros().toPlainString())).sum();
        data.put("totalAdd", add);
        data.put("totalReduce", reduce);
        return data;
    }

    /**
     * 详情
     */
    @Override
    public AppMingxiJinbi get(Integer id) {
        return afterMingxiJinbiDao.get(id);
    }

    /**
     * 添加/修改
     */
    @Override
    public void save(AppMingxiJinbi haoYou) {
        afterMingxiJinbiDao.save(haoYou);
    }

    /**
     * 添加明细
     * @param userid userid
     * @param num 数量
     * @param leixing 类型  1增加  2减少
     * @param beizhu  备注
     */
    @Override
    public void addMingxi(Integer userid,BigDecimal num,Integer leixing,String beizhu) {
        AppUser user=afterUserDao.get(userid);
        AppMingxiJinbi zuanShi=new AppMingxiJinbi();
        zuanShi.setUserid(user.getId());
        zuanShi.setPic(user.getPic());
        zuanShi.setPhone(user.getLoginname());
        zuanShi.setNick(user.getNick());
        zuanShi.setLeixing(leixing);
        zuanShi.setNum(num);
        zuanShi.setCreatetime(new Date());
        zuanShi.setBeizhu(beizhu);
        afterMingxiJinbiDao.save(zuanShi);
    }

    @Override
    public Map<String, Object> findList(String userIds) {
        Map<String, Object> map = new HashMap<>();
        List<AppMingxiJinbi> list = afterMingxiJinbiDao.findList(userIds);

        map = new HashMap<>();
        map.put("userids", userIds);
        BigDecimal bigDecimal = appMingXiShouLiService.listSum(map);
        map = new HashMap<>();
        map.put("today", "1");
        BigDecimal bigDecimalToday = appMingXiShouLiService.listSum(map);

        Double today = 0d;
        double sum = list.stream().filter(e -> e.getLeixing() == 1).mapToDouble(e -> Double.valueOf(e.getNum().stripTrailingZeros().toPlainString())).sum();

        for(AppMingxiJinbi b : list) {
            if(DatesUtil.isDay(b.getCreatetime())) {
                today += Double.valueOf(b.getNum().stripTrailingZeros().toPlainString());
            }
        }
        map.put("today", today + bigDecimalToday.doubleValue());
        map.put("sum", sum + bigDecimal.doubleValue());
        return map;
    }
}
